var express = require('express')
var app = express()
var bodyParser = require('body-parser')
var morgan = require('morgan')
var server = require('http').createServer(app)

var io = require('socket.io').listen(server)

var models = require('./collection.js')
require('./socket.js')(io, models.User, models.Info)

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extend:false}))

app.use(morgan('dev'))
app.use(express.static('static'))
app.use(express.static('static/imgs'))
app.use(express.static('lib'))
app.use(express.static('components'))

app.get('/', function (req, res) {
  res.sendfile('index.html')
})

app.post('/register', function (req, res) {
  models.User.findOne({user: req.body.user}, function (err, user) {
    if (user) {
      res.send('0')
    } else {
      (new models.User(req.body)).save(function (err, user) {
        res.send('1')
      })
    }
  })
})

server.listen(8081, function () {
  console.log('正在监听8081')
})
